<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Page File->Images
 * Simple filebrowser for the userimage-area
 * get: images.tpl.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
function contentPage($refPage, $page, $subpage, $refValidate)
{
	$upPath = '';
	
	if (!isset($_GET['path']))
		$_GET['path'] = '';
	if (!isset($_GET['display']))
		$_GET['display'] = 0;

	$actPath = $refValidate->cleanslash($_GET['path'], _('Wrong path'), 1, 64);

	if (isset($_REQUEST['submit']))
	{
		if ($refValidate->cleanslash($_REQUEST['submit']) === _('Upload'))
		{ // Upload File
			include_once('module/class/upload.class.php');
			$upload = new upload;
			if ($upload->doUpload($refValidate->cleanslash($_FILES['imagefile']['name']),
					USER_IMAGE_PATH . $actPath, ARTICLE_IMAGE_SIZE, 'imagefile'))
			{
				message::setNoteRow(_('The file was uploaded successfully.'));
			}
			else
			{
				message::setErrorRow($upload->getError());
			}
		}

		if ($refValidate->cleanslash($_REQUEST['submit']) === _('Create'))
		{ // Create direcory
			$newDir = $refValidate->cleanslash($_POST['path'], _('Wrong POST->path'), 1, 64) .
				'/' . $refValidate->cleanslash($_POST['dirx'], _('Wrong POST->dir'), 1, 64);
			if (mkdir($newDir))
			{
				message::setNoteRow(_('The directory was create successfully.'));
			}
		}
	}

	if (isset($_GET['dirdelete']))
	{ // Delete direcory
		if ($_GET['dirdelete'] == 1)
		{
			if (rmdir(USER_IMAGE_PATH . $actPath))
			{
				message::setNoteRow(_('The directory was delete successfully.'));
			}
			else
			{
				message::setErrorRow(_('The directory is not empty! <br>' .
						' Only empty directorys can be deleted!'), true);
			}
			$xPath = array();
			$xPath = explode('/', $actPath);

			if (count($xPath) >= 2)
			{
				for ($i = 0; $i <= (count($xPath) - 2); $i++)
				{
					$upPath = $upPath . $xPath[$i] . '/';
				}
			}
			$actPath = $upPath;
			$_GET['dirdelete'] = 0;
		}
	}

	if (isset($_GET['filedelete']))
	{ // Delete file
		if ($_GET['filedelete'] == 1)
		{
			if (unlink('../' . USER_IMAGE_PATH . $actPath))
			{
				message::setNoteRow(_('The file was deleted successfully.'));
			}
			else
			{
				message::setErrorRow(_('The file can not delete! <br>' .
						'(Filepermissions?)'), true);
			}
			$xPath = array();
			$xPath = explode('/', $actPath);

			if (count($xPath) >= 2)
			{
				for ($i = 0; $i <= (count($xPath) - 2); $i++)
				{
					$upPath = $upPath . $xPath[$i] . '/';
				}
			}
			$actPath = $upPath;
			$_GET['filedelete'] = 0;
		}
	}

	// If actual a subdir displayed then create a link to the 'up-dir'
	if (!empty($actPath))
	{
		if ($_GET['display'] == 1)
		{
			$xStep = 2;
		}
		else
		{
			$xStep = 3;
		}
		$xPath = array();
		$xPath = explode('/', $actPath);

		if (count($xPath) >= $xStep)
		{
			for ($i = 0; $i <= (count($xPath) - $xStep); $i++)
			{
				$upPath = $upPath . $xPath[$i] . '/';
			}
		}
	}

	$refPage->assign('sPath',
		$_SERVER['PHP_SELF'] . '?page=' . $page . '&sub=' . $subpage . '&path=' . $upPath);
	$refPage->assign('sPathLabel', '../ (up one dir)');

	if ($_GET['display'] == 1)
	{
		$refPage->assign('header', _('Image preview'));
		$refPage->assign('actualPath', _('DISPLAYED IMAGE: ') . '<br>' . $actPath);

		$refPage->assign('imageDisplay', '../' . USER_IMAGE_PATH . $actPath);
	}
	else
	{
		$refPage->assign('header', _('Imagemanager'));
		$refPage->assign('actualPath', _('ACTUAL PATH: ') . '<br>' . $actPath);

		if ($handle = opendir(('../' . USER_IMAGE_PATH . $actPath)))
		{ //Create dir- and filelist
			$dirListx = array();
			while (false !== ($file = readdir($handle)))
			{
				if ($file != "." && $file != "..")
				{
					$fName = $file;
					$file = $actPath . $file;
					$pathx = '../' . USER_IMAGE_PATH . $file;

					if (is_file($pathx))
					{
						$fileInfo = pathinfo($pathx);
						if (strtolower($fileInfo['extension']) == "jpg" or
							strtolower($fileInfo['extension']) == "gif" or
							strtolower($fileInfo['extension']) == "png")
						{
							$dirListx[] = array('icon' => 'template/images/file.image/show.gif',
							 'fileName' => $_SERVER['PHP_SELF'] . '?page=' . $page . '&sub=' . $subpage . '&path=' . $file . '&display=1',
							 'fileDelete' => $_SERVER['PHP_SELF'] . '?page=' . $page . '&sub=' . $subpage . '&path=' . $file . '&filedelete=1',
							 'fileNamex' => $fName,
							 'date' => date('d-m-Y H:i:s', filemtime($pathx)),
							 'fileSize' => filesize($pathx) . _(' bytes'));
						}
						else
						{
							$dirListx[] = array('icon' => 'template/images/file.image/lock.gif',
							 'fileName' => '#',
							 'fileDelete' => $_SERVER['PHP_SELF'] . '?page=' . $page . '&sub=' . $subpage . '&path=' . $file . '&filedelete=1',
							 'fileNamex' => $fName,
							 'date' => date('d-m-Y H:i:s', filemtime($pathx)),
							 'fileSize' => filesize($pathx) . _(' bytes'));
						}
					}
					elseif (is_dir($pathx))
					{
						$dirListx[] = array('icon' => 'template/images/file.image/dir.gif',
						 'fileName' => $_SERVER['PHP_SELF'] . '?page=' . $page . '&sub=' . $subpage . '&path=' . $file . '/' . '&display=0',
						 'fileDelete' => $_SERVER['PHP_SELF'] . '?page=' . $page . '&sub=' . $subpage . '&path=' . $file . '&dirdelete=1',
						 'fileNamex' => $fName,
						 'date' => ' ',
						 'fileSize' => ' ');
					}
				}
			}
			$refPage->assign('dirList', $dirListx);
			$refPage->assign('image', _('Upload file') . ': ');
			$refPage->assign('imagex', '<input type="file" size="25" name="imagefile" id="imagefile" />');
			$refPage->assign('dir', _('Directoryname') . ': ');
			$refPage->assign('dirx', '<input type="text" name="dirx" id="dirx" size="25" ' . 'value="" />');
		}
	}

	$refPage->assign('formStart',
		$_SERVER['PHP_SELF'] . '?page=' . $page . '&sub=' . $subpage . '&path=' . $actPath . '&upload=1');
	$refPage->assign('btnSave',
		'<input type="hidden" name="path" value="' . '../' . USER_IMAGE_PATH . $actPath . '">' .
		'<input type="submit" name="submit" id="save" value="' . _('Upload') . '" />');
	$refPage->assign('btnDir',
		'<input type="submit" name="submit" id="dir" value="' . _('Create') . '" />');
	$refPage->addtpl('content', '/file/images.tpl.php');
}

?>